package com.naja.event.tracking.extract.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clickhouse.client.internal.google.gson.JsonParser;
import com.naja.event.tracking.extract.domain.TBizEvent;
import com.naja.event.tracking.extract.repository.mybatis.TBizEventDao;
import com.naja.event.tracking.extract.service.TBizEventService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 埋点业务事件(TBizEvent)表服务实现类
 *
 * @author EasyCode
 * @since 2023-09-18 13:52:47
 */
@Service()
public class TBizEventServiceImpl extends ServiceImpl<TBizEventDao, TBizEvent> implements TBizEventService {
    /**
     * 插入（批量）
     * @param entityList 实体对象集合
     */
    @Override
    public boolean saveBatch(Collection<TBizEvent> entityList) {
        return saveBatch(entityList, DEFAULT_BATCH_SIZE);
    }

    /**
     * 插入（批量）
     * @param entityList 实体对象集合
     * @param batchSize  插入批次数量
     */
    @Override
    public boolean saveBatch(Collection<TBizEvent> entityList,int batchSize) {
        int idx = 0;
        ArrayList<TBizEvent> bizEvents = new ArrayList<>(entityList);
        int size = Math.min(bizEvents.size(), batchSize);
        do {
            List<TBizEvent> partBizEvents = bizEvents.subList(idx, Math.min(idx + size, bizEvents.size()));
            this.baseMapper.insertBatchJson(JsonParser.parseString(JSONObject.toJSONString(partBizEvents)));
            idx += size;
        } while (idx < size);
        return true;
    }

}

